﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

using LowLevelGraphics.Drawing;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Coarse filter
    /// Choose between filled squared (true) and circles (false)
    /// </summary>
    public class Coarse : BaseImageFilter
    {
        protected bool m_bPixellate = true;
        protected int m_nLength = 2;

        /// <summary>
        /// 
        /// </summary>
        public Coarse()
            : this(true, 2)
        {
        }

        /// <summary>
        /// Specify the length
        /// </summary>
        /// <param name="_nLength"></param>
        public Coarse(int _nLength)
            : this(true, _nLength)
        {
        }

        /// <summary>
        /// Specify mode and length
        /// </summary>
        /// <param name="_bPixellate"></param>
        /// <param name="_nLength"></param>
        public Coarse(bool _bPixellate, int _nLength)
        {
            m_bPixellate = _bPixellate;
            m_nLength = _nLength;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_coarse"></param>
        internal Coarse(Coarse _coarse)
        {
            this.m_bPixellate = _coarse.m_bPixellate;
            this.m_nLength = _coarse.m_nLength;
        }

        /// <summary>
        /// Choose between coarsing using squares or circles/ellipses
        /// </summary>
        public bool Pixellation
        {
            get { return m_bPixellate; }
            set { m_bPixellate = value; }
        }

        /// <summary>
        /// gets or sets the length
        /// </summary>
        public int Length
        {
            get { return m_nLength; }
            set { m_nLength = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            if (m_bPixellate)
            {
                Pixellate(_bitmap, m_nLength);
            }
            else
            {
                Circellate(_bitmap, m_nLength);
            }
            return _bitmap;
        }

        /// <summary>
        /// Executes the pixellation function
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="nLength"></param>
        /// <returns></returns>
        private static bool Pixellate(UnsafeBitmap bitmap, int nLength)
        {
            UnsafeGraphics g = new UnsafeGraphics(bitmap);

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            for (y = 0; y < nHeight; y += nLength)
            {
                for (x = 0; x < nWidth; x += nLength)
                {
                    color = bitmap.GetPixel(x, y);
                    g.FillRectangle(new SolidBrush(color), x, y, nLength, nLength);
                }
            }
            return true;
        }

        /// <summary>
        /// Executes the circellation function
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="nLength"></param>
        /// <returns></returns>
        private static bool Circellate(UnsafeBitmap bitmap, int nLength)
        {
            UnsafeGraphics g = new UnsafeGraphics(bitmap);
            
            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            Color color = Color.Transparent;
            for (y = 0; y < nHeight; y += nLength)
            {
                for (x = 0; x < nWidth; x += nLength)
                {
                    color = bitmap.GetPixel(x, y);
                    if (x < nWidth - nLength && y < nHeight - nLength && x > nLength && y > nLength)
                    {
                        g.FillCircle(new SolidBrush(color), x, y, nLength);
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// Use this to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Coarse(this);
        }
    }
}
